﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using sg.edu.nus.iss.prettyfaces.DataAccessLayer;
using sg.edu.nus.iss.prettyfaces.BusinessLogicLayer;
using sg.edu.nus.iss.prettyfaces.Models;

namespace sg.edu.nus.iss.prettyfaces.Controllers
{
    public class PrettyModelController : Controller
    {
        ModelManager manager = new ModelManager();
        MessageCompository mc = new MessageCompository();
        
        public ActionResult MessageInbox(Guid id)
        {
            //Guid id = new Guid("B1449CBE-3F37-4D80-93A8-E31B12A4A3CB");
            IEnumerable<Message> msgList = manager.getMessageById(id);
            manager.Save();
            return View(msgList);
        }

        //
        // GET: /PrettyModel/Details/5

        public ActionResult Details()
        {
            Guid id = new Guid("B1449CBE-3F37-4D80-93A8-E31B12A4A3CB");
            PrettyModel model = manager.viewModelProfile(id);
            manager.Save();
            return View(model);
        }

        public ActionResult openMessageById(Guid id)
        {
            //Guid id = new Guid("f23b6bb8-38d5-438f-9fda-120bc52830f1");
            Message msg = manager.getDetailMsssessageById(id);
            manager.Save();
            return View(msg);
        }
        //public ActionResult ApproveRejectScaffold()
        //{
        //    Guid id = new Guid("B1449CBE-3F37-4D80-93A8-E31B12A4A3CB");
        //    Relationship request = manager.getSingleRequestById(id);
        //    manager.Save();
        //    return View("ApproveRejectScaffold", request);
        //}

        public ActionResult createNewMessage(Guid id)
        {
            Message msg = new Message();
            //Guid id = new Guid("B1449CBE-3F37-4D80-93A8-E31B12A4A3CB");
            msg.SenderUserId = id;
            msg.ID = new Guid();
            msg.CreatedDate = new DateTime();
            return View(msg);
        }
        public ActionResult getRequestById(Guid id)
        {
            //Guid id = new Guid("B1449CBE-3F37-4D80-93A8-E31B12A4A3CB");
            IEnumerable<Relationship> requests = manager.getRequestById(id);
            manager.Save();
            return View(requests);

        }

        [HttpPost]
        public ActionResult createNewMessage(FormCollection collection)
        {
            try
            {
                Message msg = new Message();
                UpdateModel(msg);
                manager.createNewMessage(msg);
                manager.Save();
                return RedirectToAction("MessageInbox");
            }
            catch
            {
                return View();
            }
        }
        public ActionResult SaveApprove(Guid id)
        {
            //Guid id = new Guid("245d8081-1593-4604-8e15-0654ad709b47");
            Relationship r = new Relationship();
            r = manager.getRelationshipById(id);
            r.Status = 3;
            UpdateModel(r);
            IEnumerable<Relationship> request = manager.getRequestByPId(id);
            manager.Save();
            return View("getRequestById", request);

        }
        public ActionResult SaveReject(Guid id)
        {
            //Guid id = new Guid("245d8081-1593-4604-8e15-0654ad709b47");
            Relationship r = new Relationship();
            r = manager.getRelationshipById(id);
            r.Status = 2;
            UpdateModel(r);
            IEnumerable<Relationship> request = manager.getRequestByPId(id);
            manager.Save();
            return View("getRequestById",request);
        }

        //[HttpPost]
        //public ActionResult ApproveRejectScaffold(FormCollection collection)
        //{
        //    try
        //    {
        //        Guid id = new Guid("B1449CBE-3F37-4D80-93A8-E31B12A4A3CB");
        //        Relationship request = manager.getSingleRequestById(id);
        //        UpdateModel(request);
        //        manager.Save();

        //        return RedirectToAction("getRequestById");
        //    }
        //    catch
        //    {
        //        return RedirectToAction("ApproveRejectScaffold");
        //    }
        //}

        //
        // GET: /PrettyModel/Create

        //public ActionResult Create()
        //{
        //    return View();
        //} 

        ////
        //// POST: /PrettyModel/Create

        //[HttpPost]
        //public ActionResult Create(FormCollection collection)
        //{
        //    try
        //    {
        //        Message msg = new Message();
        //        UpdateModel(msg);
        //        manager.createNewMessage(msg);
        //        manager.Save();
        //        return RedirectToAction("MessageInbox");
        //    }
        //    catch
        //    {
        //        return View();
        //    }
        //}
        
        //
        // GET: /PrettyModel/Edit/5

        //[HttpPost]
        //public string ApproveAction(Guid id)  
        //{  
        //   return UpdateAction(id,1);  
        //}  
  
        //[HttpPost]   
        //public string RejectAction(Guid id)  
        //{  
        //   return UpdateAction(id,2);  
        //}  
  
        //protected string UpdateAction(Guid id, int status)  
        //{  
  
        //   string result = "Error: Unable to Update";  
        //    if (manager.UpdateStatus(id, 1))  
        //    {  
        //        result = "Approved";  
        //    }
        //    else if (manager.UpdateStatus(id, 2))
        //    {
        //        result = "Rejected";
        //    }
  
        //    return result;  
        //}


        public ActionResult EditScaffold(Guid id)
        {
            //Guid id = new Guid("B1449CBE-3F37-4D80-93A8-E31B12A4A3CB");
            PrettyModel model = manager.viewModelProfile(id);
            return View("EditScaffold", model);
        }

        //
        // POST: /PrettyModel/Edit/5

        [HttpPost]
        public ActionResult EditScaffold(Guid id,FormCollection collection)
        {
            try
            {
                //Guid id = new Guid("B1449CBE-3F37-4D80-93A8-E31B12A4A3CB");
                PrettyModel model = manager.viewModelProfile(id);
                UpdateModel(model);
                manager.Save();

                return RedirectToAction("Details");
            }
            catch
            {
                return View("EditScaffold");
            }
        }

        //
        // GET: /PrettyModel/Delete/5
 
        //public ActionResult Delete(int id)
        //{
        //    return View();
        //}

        ////
        //// POST: /PrettyModel/Delete/5

        //[HttpPost]
        //public ActionResult Delete(int id, FormCollection collection)
        //{
        //    try
        //    {
        //        // TODO: Add delete logic here
 
        //        return RedirectToAction("Index");
        //    }
        //    catch
        //    {
        //        return View();
        //    }
        //}
    }
}
